import org.grailsext.annotation.Privileged
import org.grailsext.util.Utils

class RoleController extends BaseController{

	def beforeInterceptor = [action:this.&auth]

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

	@Privileged(privileges = ['SHOW_ROLE'])
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [roleList: Role.list(params), roleTotal: Role.count()]
    }

	@Privileged(privileges = ['CREATE_ROLE'])
    def create = {
        def role = new Role()
        role.properties = params
        return [role: role]
    }

	@Privileged(privileges = ['CREATE_ROLE'])
    def save = {
        def role = new Role(params)
		def privIds = Utils.convertStrings2Longs(params.selectedPrivileges)
		if(privIds){
			def criteria = Privilege.createCriteria()
			def privs = criteria.list{'in'('id', privIds)}
			role.privileges = privs;
		}
        if (role.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'role.label', default: 'Role'), role.roleName])}"
            redirect(action: "show", id: role.id)
        }
        else {
            render(view: "create", model: [role: role])
        }
    }

	@Privileged(privileges = ['SHOW_ROLE'])
    def show = {
        def role = Role.get(params.id)
        if (!role) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), role.roleName])}"
            redirect(action: "list")
        }
        else {
            [role: role]
        }
    }

	@Privileged(privileges = ['UPDATE_ROLE'])
    def edit = {
        def role = Role.get(params.id)
        if (!role) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), role.roleName])}"
            redirect(action: "list")
        }
        else {
            return [role: role]
        }
    }

	@Privileged(privileges = ['UPDATE_ROLE'])
    def update = {
        def role = Role.get(params.id)
        if (role) {
            if (params.version) {
                def version = params.version.toLong()
                if (role.version > version) {
                    
                    role.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'role.label', default: 'Role')] as Object[], "Another user has updated this Role while you were editing")
                    render(view: "edit", model: [role: role])
                    return
                }
            }
            role.properties = params
			def privIds = Utils.convertStrings2Longs(params.selectedPrivileges)
			if(privIds){
				def criteria = Privilege.createCriteria()
				def privs = criteria.list{'in'('id', privIds)}
				role.privileges = privs;
			}
            if (!role.hasErrors() && role.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'role.label', default: 'Role'), role.roleName])}"
                redirect(action: "show", id: role.id)
            }
            else {
                render(view: "edit", model: [role: role])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), role.roleName])}"
            redirect(action: "list")
        }
    }

	@Privileged(privileges = ['DELETE_ROLE'])
    def delete = {
        def role = Role.get(params.id)
        if (role) {
            try {
                role.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'role.label', default: 'Role'), role.roleName])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'role.label', default: 'Role'), role.roleName])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), role.roleName])}"
            redirect(action: "list")
        }
    }
}
